package com.sx.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.dfec.cache.redis.UrlDataCash;
import com.dfec.model.Msg;
import com.dfec.utils.Consists;
import com.dfec.utils.PropertiesUtil;
import com.dfec.utils.ResponseUtil;
import com.sx.service.RealService;

/**
 * Servlet implementation class ForcastController
 */
@WebServlet(name = "real", description = "自动站与区域站实况", urlPatterns = { "/real" })
public class PositionRealController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private List<Object> list=null;
    Msg msg=null;
	RealService s=new RealService();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		msg=new Msg();
		list=new ArrayList<>();
		List<Object> getdata = null ;
		try {
			String type=request.getParameter("type");
			
			//多条件
			if(null!=type&&type.equals("search_more")) {//高级
				String	more_one=request.getParameter("more_one");//单时次还是多时次
				
				String	cnty=request.getParameter("cnty");
				String	count=request.getParameter("count");//显示条数
				String	rain=request.getParameter("rain");//雨量过滤阈值
				String	starttime=request.getParameter("starttime");//任意时次开始时间
				String	endtime=request.getParameter("endtime");//任意时次结束时间
				
				if(more_one.equals("more")) {//自定义任意时次
					getdata = s.getRain(cnty, count,rain,starttime,endtime);
				}else if (more_one.equals("one")) {//单时次
					String	times=request.getParameter("times");//单时次  1,3,6,12,24
					getdata = s.getRain(cnty, count,rain,times);
				}
				
				
			//单站自定义
			}else if(null!=type&&type.equals("search_one")) {//站名
				
				String	station_name=request.getParameter("station_name");
				getdata = s.getRain(station_name);
			}else {
				String lat=request.getParameter("lat");
				String lon=request.getParameter("lon");
				//根据经纬度获取最近值
				getdata = s.getNearPosition(lat, lon);
			}
			
			if(!getdata.get(0).toString().equals("[]")){
				msg.setCode(Consists.DATA_Code_OK);
				msg.setMsg(Consists.DATA_Msg_OK);
				
			}else {
				msg.setCode(Consists.DATA_Code_OK__NODATA);
				msg.setMsg(Consists.DATA_Msg_OK_ONDATA);
			}	
			
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(Consists.Error_Code_ServerError);
			msg.setMsg(Consists.Error_Msg_ServerError);
		}
		list.add(msg);
		list.add(getdata.get(0));
		
		if(msg.getCode().equals(Consists.DATA_Code_OK)&&PropertiesUtil.config_redis_use) {
			UrlDataCash.saveUrlData(UrlDataCash.urlKey(request).toString(), JSON.toJSONString(list));
		}
	 ResponseUtil.sendScript(response, JSON.toJSONString(list));
	}

}
